// 管理员数据管理模块

import { defineStore } from "pinia";
import { ref, reactive } from "vue";
import {
  loginApi,
  getAdminsApi,
  getAdminTotalApi,
  addAdminApi,
  delAdminApi,
  editAdminApi,
} from "../api/adminApi";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

const useAdminStore = defineStore("admin", () => {
  const router = useRouter();
  // 声明响应式数据
  let loginUser = reactive([]);
  let token = ref("");
  const adminData = reactive({
    list: [],
  });
  const page = ref(1);
  const size = ref(10);
  const total = ref(0);
  // 对话框
  const dialogVisible = ref(false);
  function closeDialog() {
    dialogVisible.value = false;
  }
  function openDialog() {
    dialogVisible.value = true;
  }
  function changePage(value) {
    page.value = value;
  }
  function changeSize(value) {
    size.value = value;
  }
  // 声明操作数据函数
  const loginAction = (data) => {
    loginApi(data).then((res) => {
      // console.log(res,'登录------')
      if (res.code === 1) {
        ElMessage({
          showClose: true,
          message: "恭喜您，登录成功！",
          type: "success",
          duration: 3000,
        });
        loginUser = res.msg;
        token = res.msg.token;
        // sessionStorage.setItem('loginUser',JSON.stringify(res.msg))
        sessionStorage.setItem("token", res.msg.token);
        // 跳转首页
        setTimeout(() => {
          router.push({ name: "home" });
        }, 500);
      } else {
        console.log(res, "登录失败------");
        ElMessage({
          showClose: true,
          message: res.msg,
          type: "error",
          duration: 3000,
        });
      }
    });
  };
  const getAdminsAction = () => {
    // 获取菜单
    getAdminsApi(page.value, size.value).then((res) => {
      // console.log(res,'管理员数据--------')
      if (res.code === 200) {
        adminData.list = res.list;
      }
    });
  };
  const getAdminsTotalAction = () => {
    // 获取菜单
    getAdminTotalApi().then((res) => {
      // console.log(res,'管理员总数--------')
      if (res.code === 200) {
        total.value = res.list[0].total;
      }
    });
  };
  const addAdminAction = (data) => {
    // 新增
    addAdminApi(data).then((res) => {
      // console.log(res,'新增管理员------')
      if (res.code === 200) {
        ElMessage({
          message: "新增成功",
          type: "success",
        });
        // 重新获取管理员数据
        getAdminsAction();
      } else {
        ElMessage({
          type: "error",
          message: res.msg,
        });
      }
    });
  };
  const delAdminAction = (id) => {
    // 删除
    delAdminApi(id).then((res) => {
      // console.log(res,'删除管理员------')
      if (res.code === 200) {
        ElMessage({
          message: "删除成功",
          type: "success",
        });
        // 重新获取管理员数据
        getAdminsAction();
      } else {
        ElMessage({
          type: "error",
          message: res.msg,
        });
      }
    });
  };
  const editAdminAction = (data) => {
    // 编辑
    editAdminApi(data).then((res) => {
      // console.log(res,'编辑管理员------')
      if (res.code === 200) {
        ElMessage({
          message: "编辑成功",
          type: "success",
        });
        // 重新获取管理员数据
        getAdminsAction();
      } else {
        ElMessage({
          type: "error",
          message: res.msg,
        });
      }
    });
  };
  // 返回数据、函数
  return {
    loginAction,
    getAdminsAction,
    adminData,
    addAdminAction,
    delAdminAction,
    editAdminAction,
    dialogVisible,
    closeDialog,
    openDialog,
    getAdminsTotalAction,
    changeSize,
    changePage,
    page,
    size,
    total,
  };
});

export default useAdminStore;
